<?php
// 本类由系统自动生成，仅供测试用途
class UsercenterAction extends Action {
    public function index(){
//        if(!isset($_SESSION['home_username'])){
//            $this->error("请登录！",U('Index/index'));
//        }
            $username=urldecode($_GET['username']);
            $info=M('personalpublishcontent');
            $user=M('User');
            $value=$user->where("username='{$username}'")->find();
            $rows=$info->where("username='{$username}'")->order(' id desc')->select();
            //查头像，用户名
            $name=$value['username'];
            $isname=$_SESSION['home_username'];
            //关注的人
            $focus=$user->where("username='{$_SESSION['home_username']}'")->getField('focusid');
            $focusall=explode(',',$focus);
            $oldfocusid=$user->where("username='$name'")->getField('id');
        //=====================================关注，已关注显示和隐藏
            if($name!=$isname){
                if(in_array($oldfocusid,$focusall)){
                    $show='display:none';
                    $show1='display:show';
                }
                else{
                    $show='display:show';
                    $show1='display:none';
                }
            }
            else{
                $show='display:none';
                $show1='display:none';
            }
        //=========================================
            $touxiang=$value['maxtouxiang'];
            $this->assign('touxiang',$touxiang);
            $this->assign('name',$name);
            $this->assign('show',$show);
            $this->assign('show1',$show1);
            //增加关注时用
            $this->assign('isname',$isname);
            $this->assign('rows',$rows);
            //跨模块调用(其实数据已在AfterLogin/index下存到数据库，也可以直接访问数据库)
            //查看微博数量，调用AfterLogin模块下的num()方法,并传入参数
            $wbnums=R('AfterLogin/num',array($username));
            //查看关注人的数量，调用AfterLogin模块下的focus()方法，并传入参数
            $fnums=R('AfterLogin/focus',array($username));
            //查看粉丝，调用AfterLogin模块下的fensi()方法，并传入参数
            $fsnums=R('AfterLogin/fensi',array($username));
            //插入数据库
            //echo $user->getLastSql();
            //超链接
            $link=M('links');
            $links = $link->order('id desc')->select();
            $this->assign('links',$links);
            //分配到模板
            $this->assign('fsnums',$fsnums);
            $this->assign('fnums',$fnums);
            $this->assign('wbnums',$wbnums);
            $this->display();
    }
    //删除微博
    public function del(){
            //从ajax接收内容
            $id= $_GET['idx'];
            $val=$_GET['val'];
            $userid=$_GET['userid'];
            $del=M('personalpublishcontent');
            $user=M('User');
            $username=$del->where("id={$id}")->getField('username');
            if($username==$_SESSION['home_username']){
                if($del->where("id={$id}")->delete()){
                    $user->where("id={$userid}")->setField('wbnums',$val);
                    echo 1;
                }
            }
            else{
                    echo '不是本人，无权删除';
                }
    }
    //点赞
    public function dianzan(){
            $idx=$_GET['idx'];
            $data=array('zan'=>$_GET['val']);
            $dianzan=M('personalpublishcontent');
            if($dianzan->where("id={$idx}")->save($data)){
                echo 1;
            }

    }
    //粉丝管理
    public function fensi(){
            $username=urldecode($_GET['username']);
            $user=M('User');
            $name=$username;
            $isname=$_SESSION['home_username'];
            $focus=$user->where("username='{$_SESSION['home_username']}'")->getField('focusid');
            $focusall=explode(',',$focus);
            $oldfocusid=$user->where("username='$name'")->getField('id');
            //=====================================关注，已关注显示和隐藏
            if($name!=$isname){
                if(in_array($oldfocusid,$focusall)){
                    $show='display:none';
                    $show1='display:show';
                }
                else{
                    $show='display:show';
                    $show1='display:none';
                }
            }
            else{
                $show='display:none';
                $show1='display:none';
            }
            $this->assign('show',$show);
            $this->assign('show1',$show1);
            //查出发布微博的ID
            $row=$user->where("username='{$username}'")->find();
            $name=$row['username'];
            $touxiang=$row['maxtouxiang'];
               
            $this->assign('touxiang',$touxiang);
            $this->assign('name',$name);
            $userid=$row['id'];
            $row=$user->field('focusid ,id')->select();
            $arr1=array();
            $arr2=array();
            foreach($row as $value){
                  $arr=explode(',',$value['focusid']);
                  //$value是一维数组，$value['id']是用户ID；
                  $a=$value['id'];
                  foreach($arr as $value){
                        if($value==$userid){
                                $arr1[]=$value;
                                //如果由字符串转成的数组中有符合条件的，则把$value['id']保存到数组中。
                                $arr2[]=$a;
                        }
                  }      
              }
            //$arr2中就是所有粉丝的ID，准备sql语句
            $where['id']=array('in',$arr2);
            $rows=$user->where($where)->select();
            //超链接
            $link=M('links');
            $links = $link->order('id desc')->select();
            $this->assign('links',$links);
            $this->assign('rows',$rows);
            $this->display();
             
    }
    //关注管理
    public function focus(){
        $username=urldecode($_GET['username']);
        $user=M('User');
        $name=$username;
        $isname=$_SESSION['home_username'];
        $focus=$user->where("username='{$_SESSION['home_username']}'")->getField('focusid');
        $focusall=explode(',',$focus);
        $oldfocusid=$user->where("username='$name'")->getField('id');
        //=====================================关注，已关注显示和隐藏
        if($name!=$isname){
            if(in_array($oldfocusid,$focusall)){
                $show='display:none';
                $show1='display:show';
            }
            else{
                $show='display:show';
                $show1='display:none';
            }
        }
        else{
            $show='display:none';
            $show1='display:none';
        }
        $this->assign('show',$show);
        $this->assign('show1',$show1);
        //查出发布微博的ID
        $row=$user->where("username='{$username}'")->find();
        $name=$row['username'];
        $touxiang=$row['maxtouxiang'];

        $userid=$row['id'];
        $row=$user->where("id={$userid}")->getField('focusid');  
        $focusid=explode(',',$row);
        $where['id']=array('in',$focusid);
        $rows=$user->where($where)->select();

        //超链接
        $link=M('links');
        $links = $link->order('id desc')->select();
        $this->assign('links',$links);
        $this->assign('rows',$rows);

        $this->assign('touxiang',$touxiang);
        $this->assign('name',$name);
        $this->assign('rows',$rows);
        $this->display();
    }
    //上传头像
     public function upload(){
            $this->display();
     }
     //处理上传头像
     public function dealUpload(){
            //接收jcrop传过来的坐标值
            $x1=$_POST['x1'];
            $x2=$_POST['x2'];
            $y1=$_POST['y1'];
            $y2=$_POST['y2'];
            $width=floor($x2-$x1);
            $height=floor($y2-$y1);
            // echo $width.'<br>';
            // echo $height.'<br>';
            // exit;
            // 引入图片操作类
            import('ORG.Util.Img');
            $Imagecut = new Img();

           //引入文件上传类
            import('ORG.Net.UploadFile');
            $upload = new UploadFile();// 实例化上传类
            $upload->maxSize  = 3145728 ;// 设置附件上传大小
            $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            $upload->savePath =  './Public/uploads/';// 设置附件上传目录
            if(!$upload->upload()) {// 上传错误提示错误信息
                        $this->error($upload->getErrorMsg());
            }else{// 上传成功 获取上传文件信息
                        $info =  $upload->getUploadFileInfo();
                        $imgname = $info[0]['savename'];//获取上传后的图片名称
                        $path=$info[0]['savepath'];//获取图片上传后的文件夹
                        $imgpath = $path.$imgname;//获取上传后完整的图片地址
                        // 裁剪图片
                        $rename=$Imagecut->cut($imgpath,$x1,$y1,$width,$height);
                        // 缩略图片
                        import('ORG.Util.Image');
                        // 65px小图片
                        $mininame = $path.'mini_'.$imgname;
                        $mininame = Image::thumb($imgpath,$mininame,'','65','65');
                        // 生成小图片数据库路径
                        $miniimg = 'mini_'.$imgname;
                        // 100px大图片
                        $maxname = $path.'max_'.$imgname;
                        $maxname =  Image::thumb($imgpath,$maxname,'','100','100');
                        // 生成大图片数据库路径
                        $maximg = 'max_'.$imgname;
            }
            //图片插入数据库
            $user=M('User');
            $view=M('personalpublishcontent');
            $username=$_SESSION['home_username'];
            //查出旧图片
            $field=array('touxiang','minitouxiang','maxtouxiang');
            $oldthings=$user->where("username='{$username}'")->field($field)->find();
            $minioldtouxiang=$oldthings['minitouxiang'];
            $maxoldtouxiang=$oldthings['maxtouxiang'];
            $oldtouxiang=$oldthings['touxiang'];
            //查出旧图片完整地址
            $oldtouxiang="./Public/uploads/".$oldtouxiang;
            $minioldtouxiang="./Public/uploads/".$minioldtouxiang;
            $maxoldtouxiang="./Public/uploads/".$maxoldtouxiang;
            //更新图片
            $data=array('touxiang'=>$imgname,'minitouxiang'=>$miniimg,'maxtouxiang'=>$maximg);
            $r=$user->where("username='{$username}'")->save($data);
            //$v=$view->where("username='{$username}'")->save($data);
            if($r){
                        @unlink($minioldtouxiang);
                        @unlink($maxoldtouxiang);
                        @unlink($oldtouxiang);
                        $_SESSION['face65']=$miniimg;
                        $_SESSION['face100']=$maximg;
                         //echo    1;
                        $this->success("上传成功",U('AfterLogin/index'));
            }
            else{
                echo '上传失败';
            }

     }
    //增加关注
    public function addfocus()
    {
        $name = $_POST['name'];
        $username = $_POST['isname'];
        $user = M('User');
        $userinfo = $user->where("username='{$name}'")->field('id')->find();
        $focusid = $userinfo['id'];
        $oldfocus = $user->where("username='{$username}'")->getField('focusid');
        $oldfocusarr=explode(',',$oldfocus);
        if($oldfocus==''){
            $newfocus =$focusid;
            $re = $user->where("username='{$username}'")->setField('focusid', $newfocus);
            if ($re == 1) {
                $data = array(
                    'status' => 1,
                    'msg'=>'关注成功！'
                );
            }
            else{
                $data = array(
                    'status' => 0,
                    'msg'=>'关注失败！'
                );
            }
        }
        else{
            if(!in_array($focusid,$oldfocusarr)){
                $newfocus =$oldfocus.','.$focusid;
                $re = $user->where("username='{$username}'")->setField('focusid', $newfocus);
                if ($re == 1) {
                    $data = array(
                        'status' => 1,
                        'msg'=>'关注成功！'
                    );
                }
                else{
                    $data = array(
                        'status' => 0,
                        'msg'=>'关注失败！'
                    );
                }
            }
            else{
                $data = array(
                    'status' => 1,
                    'msg'=>'已关注！'
                );
            }
        }

        echo json_encode($data);
    }
    //转发微博
    public function forward(){
        $id=$_GET['val'];
        //根据ajax传过来的id查对应的微博内容
        $forwardwb=M('personalpublishcontent');
        $content=$forwardwb->where("id={$id}")->getField('content');
        $data['content']=$content;
        $username=$_SESSION['home_username'];
        $user=M('User');
        //查出当前用户ID
        $row=$user->where("username='{$username}'")->find();
        $userid=$row['id'];
        $data['minitouxiang']=$_SESSION['face65'];
        $data['maxtouxiang']=$_SESSION['face100'];
        $data['touxiang']=substr( $data['maxtouxiang'],strpos($data['maxtouxiang'],'_')+1);
        $data['username']=$_SESSION['home_username'];
        $data['zan']=0;
        $data['time']=date('Y-m-d H:i',time());
        $data['user_id']=$userid;

        if($forwardwb->add($data)){
            $info=[
                'status'=>1,
                'msg'=>'转发成功'
            ];
        }
        else{
            $info=[
                'status'=>0,
                'msg'=>'转发失败'
            ];
        }
        echo json_encode($info);
    }
}